package com.foxconn.ipebg.common.system.controller;

import ipebg.mzk.facade.entity.MsAdminRoleAuthority;
import ipebg.mzk.facade.entity.MsFunctionOption;
import ipebg.mzk.facade.entity.MsFunctionRoleOption;
import ipebg.mzk.facade.system.bean.FunctionOptionBean;
import ipebg.mzk.facade.system.service.FunctionService;
import ipebg.mzk.facade.system.service.MsFunctionOptionService;
import ipebg.mzk.facade.system.service.MsFunctionRoleOptionService;
import ipebg.mzk.facade.system.service.RoleService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;


/**
 * 對角色菜單操作方法
 * @author F3225975
 *
 */
@Controller
@RequestMapping("/roleauth.do")
public class RoleAuthrityAction {
	 
	 @Resource(name="msFunctionOptionServiceImpl")
	 MsFunctionOptionService functionOptionService;
	 
	 @Resource(name="roleServiceImpl")
	 RoleService roleService;
	 
	 @Resource(name="functionServiceImpl")
	 FunctionService functionService;
	 
	 @Resource(name="msFunctionRoleOptionServiceImpl")
	 MsFunctionRoleOptionService functionRoleOptionService;
	 
	 
	/**
	 * 加載角色權限設置頁面方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=load")
	public String load(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/pages/common/system/role_function_option";
	}
	 
	/**
	 * 保存角色權限方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=saveauth")
	public void saveAuth(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String functionId = request.getParameter("functionId");
		String roleId = request.getParameter("roleId");
		String[] functionIds = functionId.split(",");
		try {
			List<MsAdminRoleAuthority> roleAuthorityList = roleService.findAuthorityByRoleId(roleId);
			functionService.updateRoleFunctionByDBRoleFunction(roleAuthorityList, functionIds, roleId);
			response.getWriter().write("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存角色權限按鈕方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(params = "action=saveauthOption")
	public void saveAuthOption(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String functionId = request.getParameter("functionId");
		String roleId = request.getParameter("roleId");
		String options = request.getParameter("optionIds");
		String[] optionIds = options.contains(",") ? options.split(",") : new String[]{options};
		
		try {
			
			functionRoleOptionService.deleteEntity(functionId, roleId);
			
			for(int i=0;i<optionIds.length;i++){
				MsFunctionRoleOption bean = new MsFunctionRoleOption();
				bean.setFunctionId(functionId);
				bean.setRoleId(roleId);
				bean.setOptionId(optionIds[i]);
				functionRoleOptionService.saveEntity(bean);
			}
			response.getWriter().write("success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 獲取某一角色下具備的權限按鈕列表方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(params = "action=authOptionList")
	public void authOptionList(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		String functionId = request.getParameter("functionId");
		String roleId = request.getParameter("roleId");
		try {
			List<MsFunctionOption> optionList = functionService.findOptionByFunctionId(functionId);
			List<MsFunctionRoleOption> functionRoleOptionList = functionRoleOptionService.findOptionsByFunctionIdRoleId(functionId, roleId);
			
			List<String> array = new ArrayList<String>();
			for(MsFunctionRoleOption functionRoleOption : functionRoleOptionList){
				array.add(functionRoleOption.getOptionId());
			}
			
			List<FunctionOptionBean> beanList = new ArrayList<FunctionOptionBean>();
			for(MsFunctionOption functionOption : optionList){
				FunctionOptionBean bean = new FunctionOptionBean();
				bean.setFunctionId(functionOption.getFunctionId());
				bean.setOptionDsc(functionOption.getOptionDsc());
				bean.setOptionId(functionOption.getOptionId());
				if(array.contains(functionOption.getOptionId())){
					bean.setChecked(true);
				}else
					bean.setChecked(false);
				
				beanList.add(bean);
			}

			Map objMap = new HashMap();
			objMap.put("rows", beanList);
			objMap.put("total", beanList.size());
			
			response.getWriter().write(JSON.toJSONString(objMap));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
